<template>
    <view class="login-content">
        <view class="login-title">欢迎登录万选优品</view>
        <view class="login-logo">
            <up-image src="/static/icon/logo.png" width="136rpx" height="144rpx"></up-image>
            <view class="login-logo-text">万选优品回收平台</view>
        </view>

        <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="login-button">
            手机号码一键登录
        </button>

        <view class="login-button-text" @click="back">
            拒绝登录，继续以游客身份进行访问
        </view>
    </view>
</template>

<script setup lang="js">
import { userApi } from '@/api/user';
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'

onShareAppMessage(() => {
  return {
    title: '万选回收',
    path: '/pages/login'
  }
})

onShareTimeline(() => {
  return {
    title: '万选回收',
  }
})

const getPhoneNumber = (e) => {
    console.log(e, "e");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
        setTimeout(() => {
            wechatLogin(e.detail);
        }, 100);
    }
};

const back = () => {
    uni.navigateBack()
}

const wechatLogin = (phoneInfo) => {
    uni.login({
        provider: "weixin",
        success: (res) => {
            console.log(res.code, phoneInfo, "res")
         
            userApi
                .wechatLogin({
                    code: res.code,
                    phoneData: {
                        cloudID: phoneInfo.cloudID,
                        code: phoneInfo.code,
                        encryptedData: phoneInfo.encryptedData,
                        iv: phoneInfo.iv,
                    }
                })
                .then((res) => {
                    if (res.code == 1) {
                        const userInfo = res.data.userinfo;
                        uni.setStorageSync('token', userInfo.token);
                        uni.setStorageSync('userInfo', userInfo);
                        uni.switchTab({
                            url: '/pages/tabbar/my'
                        })
                    } else {
                        uni.showToast({ title: res.msg, icon: "none" });
                        throw new Error(res.msg);
                    }
                })
                .catch((err) => {
                    uni.hideLoading();
                    uni.showToast({ title: err, icon: "none" });
                    console.log(err, "err");
                });
        },
    });
}

</script>

<style scoped lang="scss">
@import '@/index.scss';



.login-content {
    padding: 140rpx 96rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.login-title {
    font-weight: 600;
    font-size: 48rpx;
    color: #000000;
}

.login-logo {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-top: 126rpx;
}

.login-logo-text {
    font-size: 28rpx;
    color: #000;
    margin-top: 16rpx;
}

.login-button {
    background-color: $primary;
    color: #fff;
    margin-top: 134rpx;
    border-radius: 48rpx;
    width: 100%;
    height: 96rpx;
    line-height: 96rpx;
    font-size: 28rpx;
}

.login-button-text {
    font-size: 24rpx;
    color: #999999;
    text-align: center;
    margin-top: 112rpx;
}
</style>

<style>
page {
    background-color: #fff;
}
</style>